//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "AnalyticBSMHullWhiteEngine.h"
using namespace Cephei::QL::Pricingengines::Vanilla;
#include <gen/QL/Processes/GeneralizedBlackScholesProcess.h>
#include <gen/QL/Models/Shortrate/Onefactormodels/HullWhite.h>
#include <gen/QL/PricingEngine.h>
using namespace Cephei::QL::Processes;
using namespace Cephei::QL::Models::Shortrate::Onefactormodels;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Vanilla::CAnalyticBSMHullWhiteEngine::CAnalyticBSMHullWhiteEngine (Double equityShortRateCorrelation, Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process, Cephei::QL::Models::Shortrate::Onefactormodels::IHullWhite^ model) : CPricingEngine(CAnalyticBSMHullWhiteEngine::typeid)
{
    CGeneralizedBlackScholesProcess^ _Cprocess;
    CHullWhite^ _Cmodel;
    try
    {
#ifdef HANDLE
        _phAnalyticBSMHullWhiteEngine = NULL;
#endif
        QuantLib::Real _equityShortRateCorrelation = (QuantLib::Real)ValueHelper::Convert (equityShortRateCorrelation);
        _Cprocess = safe_cast<CGeneralizedBlackScholesProcess^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>& _process = static_cast<boost::shared_ptr<QuantLib::GeneralizedBlackScholesProcess>&> (_Cprocess->GetShared ()); 
        _Cmodel = safe_cast<CHullWhite^> (model);
        _Cmodel->Lock();
        boost::shared_ptr<QuantLib::HullWhite>& _model = static_cast<boost::shared_ptr<QuantLib::HullWhite>&> (_Cmodel->GetShared ()); 
        _ppAnalyticBSMHullWhiteEngine = new boost::shared_ptr<QuantLib::AnalyticBSMHullWhiteEngine> (new QuantLib::AnalyticBSMHullWhiteEngine ( _equityShortRateCorrelation,  _process,  _model ));
        SetPricingEngine (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticBSMHullWhiteEngine));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
        if (_Cmodel != nullptr) _Cmodel->Unlock();
    }
}
Cephei::QL::Pricingengines::Vanilla::CAnalyticBSMHullWhiteEngine::CAnalyticBSMHullWhiteEngine (boost::shared_ptr<QuantLib::AnalyticBSMHullWhiteEngine>& childNative, Object^ owner) : CPricingEngine(CAnalyticBSMHullWhiteEngine::typeid)
{
#ifdef HANDLE
	_phAnalyticBSMHullWhiteEngine = NULL;
#endif
	_ppAnalyticBSMHullWhiteEngine = &childNative;
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticBSMHullWhiteEngine));
}
Cephei::QL::Pricingengines::Vanilla::CAnalyticBSMHullWhiteEngine::CAnalyticBSMHullWhiteEngine (QuantLib::AnalyticBSMHullWhiteEngine& childNative, Object^ owner) : CPricingEngine(CAnalyticBSMHullWhiteEngine::typeid)
{
#ifdef HANDLE
	_phAnalyticBSMHullWhiteEngine = NULL;
#endif
	_ppAnalyticBSMHullWhiteEngine = new boost::shared_ptr<QuantLib::AnalyticBSMHullWhiteEngine> (&childNative);
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticBSMHullWhiteEngine));
    _AnalyticBSMHullWhiteEngineOwner = owner;
    _PricingEngineOwner = owner;
}

Cephei::QL::Pricingengines::Vanilla::CAnalyticBSMHullWhiteEngine::CAnalyticBSMHullWhiteEngine (CAnalyticBSMHullWhiteEngine^ copy) : CPricingEngine(CAnalyticBSMHullWhiteEngine::typeid)
{
#ifdef HANDLE
	_phAnalyticBSMHullWhiteEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppAnalyticBSMHullWhiteEngine = new boost::shared_ptr<QuantLib::AnalyticBSMHullWhiteEngine> (copy->GetShared());
        _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticBSMHullWhiteEngine));
    }
}
Cephei::QL::Pricingengines::Vanilla::CAnalyticBSMHullWhiteEngine::CAnalyticBSMHullWhiteEngine (System::Type^ t) : CPricingEngine(CAnalyticBSMHullWhiteEngine::typeid)
{
#ifdef HANDLE
	_phAnalyticBSMHullWhiteEngine = NULL;
#endif
	if (!t->IsSubclassOf(CAnalyticBSMHullWhiteEngine::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Vanilla::CAnalyticBSMHullWhiteEngine::CAnalyticBSMHullWhiteEngine (QuantLib::Handle<QuantLib::AnalyticBSMHullWhiteEngine>& childNative, Object^ owner)  : CPricingEngine(CAnalyticBSMHullWhiteEngine::typeid)
{
	_phAnalyticBSMHullWhiteEngine = &childNative;
	_ppAnalyticBSMHullWhiteEngine = &static_cast<boost::shared_ptr<QuantLib::AnalyticBSMHullWhiteEngine>>(childNative.currentLink());
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticBSMHullWhiteEngine));
    _AnalyticBSMHullWhiteEngineOwner = owner;
}
Cephei::QL::Pricingengines::Vanilla::CAnalyticBSMHullWhiteEngine::CAnalyticBSMHullWhiteEngine (QuantLib::Handle<QuantLib::AnalyticBSMHullWhiteEngine> childNative)  : CPricingEngine(CAnalyticBSMHullWhiteEngine::typeid)
{
	_phAnalyticBSMHullWhiteEngine = &childNative;
	_ppAnalyticBSMHullWhiteEngine = &static_cast<boost::shared_ptr<QuantLib::AnalyticBSMHullWhiteEngine>>(childNative.currentLink());
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticBSMHullWhiteEngine));
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Vanilla::CAnalyticBSMHullWhiteEngine::CAnalyticBSMHullWhiteEngine (QuantLib::AnalyticBSMHullWhiteEngine childNative)  : CPricingEngine(CAnalyticBSMHullWhiteEngine::typeid)
{
#ifdef HANDLE
	_phAnalyticBSMHullWhiteEngine = NULL;
#endif
	_ppAnalyticBSMHullWhiteEngine = new boost::shared_ptr<QuantLib::AnalyticBSMHullWhiteEngine> (new QuantLib::AnalyticBSMHullWhiteEngine (childNative));
    _ppPricingEngine = new boost::shared_ptr<QuantLib::PricingEngine> (boost::dynamic_pointer_cast<QuantLib::PricingEngine> (*_ppAnalyticBSMHullWhiteEngine));
}
#endif

Cephei::QL::Pricingengines::Vanilla::CAnalyticBSMHullWhiteEngine::~CAnalyticBSMHullWhiteEngine ()
{
    if (_ppAnalyticBSMHullWhiteEngine != NULL)
    {
	    delete _ppAnalyticBSMHullWhiteEngine;
        _ppAnalyticBSMHullWhiteEngine = NULL;
    }
}
Cephei::QL::Pricingengines::Vanilla::CAnalyticBSMHullWhiteEngine::!CAnalyticBSMHullWhiteEngine ()
{
    if (_ppAnalyticBSMHullWhiteEngine != NULL)
    {
	    delete _ppAnalyticBSMHullWhiteEngine;
    }
}
QuantLib::AnalyticBSMHullWhiteEngine& Cephei::QL::Pricingengines::Vanilla::CAnalyticBSMHullWhiteEngine::GetReference ()
{
    if (_ppAnalyticBSMHullWhiteEngine == NULL) throw gcnew NativeNullException ();
	return **_ppAnalyticBSMHullWhiteEngine;
}
boost::shared_ptr<QuantLib::AnalyticBSMHullWhiteEngine>& Cephei::QL::Pricingengines::Vanilla::CAnalyticBSMHullWhiteEngine::GetShared ()
{
    if (_ppAnalyticBSMHullWhiteEngine == NULL) throw gcnew NativeNullException ();
	return *_ppAnalyticBSMHullWhiteEngine;
}
QuantLib::AnalyticBSMHullWhiteEngine* Cephei::QL::Pricingengines::Vanilla::CAnalyticBSMHullWhiteEngine::GetPointer ()
{
    if (_ppAnalyticBSMHullWhiteEngine == NULL) throw gcnew NativeNullException ();
	return &**_ppAnalyticBSMHullWhiteEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::AnalyticBSMHullWhiteEngine>& Cephei::QL::Pricingengines::Vanilla::CAnalyticBSMHullWhiteEngine::GetHandle ()
{
	if (_phAnalyticBSMHullWhiteEngine == NULL)
	{
		_phAnalyticBSMHullWhiteEngine = new Handle<QuantLib::AnalyticBSMHullWhiteEngine> (*_ppAnalyticBSMHullWhiteEngine);
	}
	return *_phAnalyticBSMHullWhiteEngine;
}
#endif
bool Cephei::QL::Pricingengines::Vanilla::CAnalyticBSMHullWhiteEngine::HasNative () 
{
	return (_ppAnalyticBSMHullWhiteEngine != NULL);
}

Cephei::QL::Pricingengines::Vanilla::IAnalyticBSMHullWhiteEngine^ Cephei::QL::Pricingengines::Vanilla::CAnalyticBSMHullWhiteEngine::Calculate::get ()
{
    try
    {
    	(*_ppAnalyticBSMHullWhiteEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Vanilla::IAnalyticBSMHullWhiteEngine^ Cephei::QL::Pricingengines::Vanilla::CAnalyticBSMHullWhiteEngine_Factory::Create (Double equityShortRateCorrelation, Cephei::QL::Processes::IGeneralizedBlackScholesProcess^ process, Cephei::QL::Models::Shortrate::Onefactormodels::IHullWhite^ model)
{
    return gcnew CAnalyticBSMHullWhiteEngine ( equityShortRateCorrelation,  process,  model);
}
